using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;

namespace Demo3Layer
{
    public class CPhanSoDAO
    {
        public CPhanSoInfo Doc(string sfileName)
        {
            CPhanSoInfo info = new CPhanSoInfo();
            XmlDocument psDoc = new XmlDocument();
            psDoc.Load(sfileName); // mo file XML
            if (psDoc != null)
            {
                XmlElement e = psDoc.DocumentElement;
                info.tu = int.Parse(e.SelectSingleNode("TUSO").InnerText);
                info.mau = int.Parse(e.SelectSingleNode("MAUSO").InnerText);
            }
            return info;
        }

        public void Ghi(string sfileName, CPhanSoInfo info)
        {
            try
            {
                XmlDocument docPhanSo = new XmlDocument();
                XmlElement rootDocXML = docPhanSo.CreateElement("zkday");
                docPhanSo.AppendChild(rootDocXML);
                XmlElement ele_Tuso = rootDocXML.OwnerDocument.CreateElement("TUSO");
                ele_Tuso.InnerText = info.tu.ToString();
                rootDocXML.AppendChild(ele_Tuso);
                XmlElement ele_Mauso = rootDocXML.OwnerDocument.CreateElement("MAUSO");
                ele_Mauso.InnerText = info.mau.ToString();
                rootDocXML.AppendChild(ele_Mauso);

                docPhanSo.Save(sfileName);
            }
            catch (Exception e)
            {
                throw new Exception("Loi trong qua trinh ghi du lieu:"+e.Message.ToString());
            }
        }
    }
}
